package cn.com.header.core.util;

import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.CellStyle;

/** 
 * 导出报表
 *
 *  
 */
public class ExportUtil {
	private String srcXlsPath="";
	private String fileName="";
	private String sheetName="";
	POIFSFileSystem fs=null;
	HSSFWorkbook wb= null;
	HSSFSheet sheet = null;
	public ExportUtil(String srcXlsPath,String fileName,String sheetName){
		this.srcXlsPath=srcXlsPath;
		this.setFileName(fileName);
		this.sheetName=sheetName;
		getSheet();
	}
	public ExportUtil(){}
	public void getSheet() {
		try{
			InputStream input =	ExportUtil.class.getResourceAsStream(srcXlsPath);
			if(input==null){
				//System.out.println("模板文件："+srcXlsPath+"不存在");
				return;
			}
			fs =new POIFSFileSystem(input);
			wb= new HSSFWorkbook(fs);
			sheet= wb.getSheet(sheetName);
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	public Integer createRow(List<Map<String,Object>> list ,String args[],int first){
		HSSFCell cell;
		CellStyle cellStyle =createBorderCellStyle();
		for(int i=0;i<list.size();i++){
			HSSFRow row=sheet.createRow(first+i);
			/*cell=row.createCell(0);
			cell.setCellStyle(cellStyle);
			cell.setCellValue(i+1);*/
			for(int j=0;j<args.length;j++){
				cell=row.createCell(j);
				cell.setCellStyle(cellStyle);
				Object o= list.get(i).get(args[j]);
				if(o!=null)cell.setCellValue(o.toString());
			}
			if(i==list.size()-1)return row.getRowNum();
		}

		return null;
	}
	
	public Integer createRow(List<Map<String,Object>> list ,String args[],int first, String paramName){
		HSSFCell cell;
		CellStyle cellStyle =createBorderCellStyle();
		for(int i=0;i<list.size();i++){
			HSSFRow row=sheet.createRow(first+i);
			cell=row.createCell(0);
			cell.setCellStyle(cellStyle);
			cell.setCellValue(i+1);
			for(int j=0;j<args.length;j++){
				cell=row.createCell(j+1);
				cell.setCellStyle(cellStyle);
				Object o= list.get(i).get(args[j]);
				if(o!=null)cell.setCellValue(o.toString());
			}
			cell=row.createCell(args.length+1);
			cell.setCellStyle(cellStyle);
			cell.setCellValue(paramName);
			if(i==list.size()-1)return row.getRowNum();
		}
		return null;
	}
	/**
	 * 写入excel文件异常信息
	 * @param list
	 * @param args
	 * @param first
	 * @return
	 */
	public Integer createRowErr(List<List<String>> list ,String args[],int first){
		HSSFCell cell;
		CellStyle cellStyle =createBorderCellStyle();
		for(int i=0;i<list.size();i++){
			HSSFRow row=sheet.createRow(first+i);
			cell=row.createCell(0);
			cell.setCellStyle(cellStyle);
			cell.setCellValue(i+1);
			for(int j=0;j<args.length;j++){
				cell=row.createCell(j+1);
				cell.setCellStyle(cellStyle);
				List<String> strList= list.get(i);
				if(strList!=null)cell.setCellValue(strList.toString());
			}
			if(i==list.size()-1)return row.getRowNum();
		}
		return null;
	}
	/**
	 * @param
	 * @param
	 * @param ,
	 */
	public void createRow(Map<Integer,Object>m,int args[],int current){
		HSSFCell cell;
		HSSFRow row=sheet.getRow(current);
		CellStyle cellStyle = createBorderCellStyle();
		for(int i=0;i<args.length;i++){
			cell=row.getCell(args[i]);
			String a=cell.getStringCellValue();
			cell.setCellStyle(cellStyle);
			if(!a.isEmpty())cell.setCellValue(a+m.get(args[i]).toString());
			else cell.setCellValue(m.get(args[i]).toString());
		}
	}
	/**
	 * 在rowNum行插入数据 
	 */
	public void createRow(Map<Integer,String> m,int rowNum){
		HSSFCell cell;
		HSSFRow row=sheet.createRow(rowNum);
		for(Map.Entry<Integer, String> entry : m.entrySet()){
			cell = row.createCell(entry.getKey());
			cell.setCellValue(entry.getValue());
		}
	}
	public void createRow(Map<Integer,String> m,int rowNum,String paramName){
		HSSFCell cell;
		HSSFRow row=sheet.createRow(rowNum);
		int i = 0;
		for(Map.Entry<Integer, String> entry : m.entrySet()){
			cell = row.createCell(entry.getKey());
			cell.setCellValue(entry.getValue());
			i++;
		}
		cell = row.createCell(i);
		cell.setCellValue(paramName);
	}
	public  CellStyle createBorderCellStyle(){
		CellStyle cs=wb.createCellStyle();
		cs.setBorderLeft(CellStyle.BORDER_THIN);
		cs.setBorderRight(CellStyle.BORDER_THIN);
		cs.setBorderTop(CellStyle.BORDER_THIN);
		cs.setBorderBottom(CellStyle.BORDER_THIN);
		cs.setWrapText(true);
		return cs;
	}
	public void setSrcXlsPath(String srcXlsPath) {
		this.srcXlsPath = srcXlsPath;
	}

	public void setSheetName(String sheetName) {
		this.sheetName = sheetName;
	}
	//下载
	public void downloadFile(HttpServletResponse response){
        try{
        	response.setContentType("application/x-msdownload");
        	response.addHeader("Content-Disposition","attachment;filename="+new String((fileName).getBytes("GB2312"),"iso8859-1")); 
			OutputStream output=response.getOutputStream();
			wb.write(output);
			response.flushBuffer();
			output.close();
        }catch(Exception e){
        	e.printStackTrace();
        }
       
	}
	public String getFileName() {
		return fileName;
	}
	public void setFileName(String fileName) {
		this.fileName = fileName;
	}
}
